package com.yingran.common.util;

import android.app.Activity;

import java.util.Stack;

/**
 * 模拟Android Activity栈, 方便管理Activity
 */
public final class AppManager {

    private static Stack<Activity> mStackList;

    private static AppManager instance;

    private AppManager() {
    }

    public static AppManager getInstance() {
        if (instance == null) {
            synchronized (AppManager.class) {
                if (instance == null) {
                    instance = new AppManager();
                    return instance;
                }
            }
        }
        return instance;
    }

    public void addActivity(Activity activity) {
        if (mStackList == null) {
            mStackList = new Stack<>();
        }
        mStackList.add(activity);
    }


    public Activity getTopActivity() {
        if (mStackList == null || mStackList.isEmpty())
            return null;
        return mStackList.lastElement();
    }


    public void finishActivity(Activity activity) {
        if (mStackList == null || mStackList.isEmpty()) {
            return;
        }
        if (activity != null) {
            mStackList.remove(activity);
            activity.finish();
        }
    }

}